package com.bawei.module_message.http

import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.module_message.model.MessageModelItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject
@HiltViewModel
class MessageViewModel@Inject constructor(private val repository: MessageRepository) :BaseViewModel() {


    val messageList= mutableListOf<MessageModelItem>()
    private val _message= MutableStateFlow<RequestEvent>(Loading)
    val messageState
        get() = _message.asStateFlow()
    fun getMessage(accessId: String, userId: Int) = execute(
        action = repository.getMessage(accessId, userId),
        onStart = {
            _message.value=Loading
        },
        onError = {
            _message.value=Failed(it)
        },
        onFinish = {
            messageList.clear()
            messageList.addAll(it.data)
            _message.value=Success
        }
    )
}